package com.arch.dev.server.report;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import com.arch.dev.client.vo.BudgetVO;

public abstract class ReportFactory {
	
	Logger logger = Logger.getLogger(ReportFactory.class);	
	
	public final String  JASPER_COMPILED_REPORT = "ExpenditureReport.jasper";

	public final String  JASPER_PRINT_REPORT = "ExpenditureReport.jrprint";
	
	public String JASPER_COMPILED_REPORT_PATH = "";
	
	public String JASPER_PRINT_REPORT_PATH = "";
	
	public String JASPER_IMAGE_PATH = "";

	public static enum ReportType {
		Excel, PDF;
	}

	public final void getReport(Collection<BudgetVO> budgetVOs,
			String destFileName, String realPath)

	throws JRException {
		JASPER_COMPILED_REPORT_PATH = realPath + "JasperReports/" + JASPER_COMPILED_REPORT;
		JASPER_PRINT_REPORT_PATH = realPath + "JasperReports/" + JASPER_PRINT_REPORT;
		JASPER_IMAGE_PATH = realPath + "JasperReports/";
		logger.info("Compiled report path : " + JASPER_COMPILED_REPORT_PATH);
		logger.info("print report path : " + JASPER_PRINT_REPORT_PATH);
		fill(budgetVOs);
		createReport(destFileName);
	}

	public void fill(Collection<BudgetVO> budgetVOs) throws JRException {
		long start = System.currentTimeMillis();
		// Preparing parameters
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("IMAGE_ROOT_DIR",JASPER_IMAGE_PATH);
		//parameters.put("DataFile", "CustomBeanFactory.java - Bean Collection");
		JasperFillManager.fillReportToFile(JASPER_COMPILED_REPORT_PATH, parameters,
				new JRBeanCollectionDataSource(budgetVOs));

		// JasperFillManager.fillReportToFile("resources/JasperReports/ExpenditureReport.jasper",
		// parameters, new
		// JRBeanCollectionDataSource(CustomBeanFactory.getBeanCollection()));
		System.err.println("Filling time : "
				+ (System.currentTimeMillis() - start));
	}

	public abstract void createReport(String destFileName) throws JRException;

}
